<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="好友名" prop="attentionUserId">
        <el-input
          v-model="queryParams.nickName"
          placeholder="请输入关注者"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['petSys:attention:add']"
        >添加好友</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-s-promotion"
          size="mini"
          @click="handleApply(1)"
        >我的申请</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="el-icon-s-comment"
          size="mini"
          @click="handleApply(2)"
          v-hasPermi="['petSys:attention:add']"
        >申请列表</el-button>
      </el-col>

      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="attentionList" @selection-change="handleSelectionChange">
<!--      <el-table-column type="selection" width="55" align="center" />-->
      <el-table-column label="用户名" align="center" prop="friendNickName" />
      <el-table-column label="备注" align="center" prop="remark" />


      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['petSys:attention:edit']"
          >备注</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['petSys:attention:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加好友对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="用户名" prop="attentionUserId">
          <el-input style="width: 60%;margin-right: 20px" v-model="queryFrienName" placeholder="请输入用户名" />
          <el-button  type="primary" icon="el-icon-search" size="mini" @click="queryFreiend">搜索</el-button>
        </el-form-item>
        <el-form-item  label="" prop="beAttentionUserId">
<!--          <el-input v-model="form.beAttentionUserId" placeholder="请输入被关注者" />-->
        <div v-for = "item in friendList"  style=" width: 250px; height:40px ;padding:5px; border-radius: 10px;border:1px solid #bfbfbf;display: flex;justify-content:space-evenly; align-content:center;margin-top: 10px">
          <div>{{item.nickName}}</div>
          <el-button   style="height: 30px;margin-left: 20px" type="primary" icon="el-icon-plus" size="mini" @click="addFriend(item)">添加</el-button>
        </div>
        </el-form-item>

      </el-form>
    </el-dialog>
    <!-- 好友申请对话框 -->
    <el-dialog title="好友申请" :visible.sync="applyOpen" width="500px"  append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">

        <el-form-item  label="" prop="beAttentionUserId">
          <!--          <el-input v-model="form.beAttentionUserId" placeholder="请输入被关注者" />-->
          <div v-for = "item in applyList"  style=" width: 250px; height:40px ;padding:5px; border-radius: 10px;border:1px solid #bfbfbf;display: flex;justify-content:space-evenly; align-content:center;margin-top: 10px">
            <div v-if="applyflag==2">{{item.applyNickName}}</div>
            <div v-if="applyflag==1">{{item.beApplyNickName}}</div>
            <el-button v-if="applyflag==2 && item.isAgree==0" style="height: 30px;justify-items: center; align-content:center" type="primary"  size="mini" @click="updateFriendApply(1,item)">同意</el-button>
            <el-button v-if="applyflag==2 && item.isAgree==0" style="height: 30px;justify-items: center; align-content:center" type="primary"  size="mini" @click="updateFriendApply(2,item)">拒绝</el-button>
            <div style="color: #13ce66" v-if="item.isAgree==1">已同意</div>
            <div style="color:red" v-if="item.isAgree==2">已拒绝</div>
            <div style="color: #bfbfbf" v-if="applyflag==1&&item.isAgree==0">未同意</div>
          </div>
          <div v-if="applyList.length==0"><el-empty description="暂无申请"></el-empty></div>
        </el-form-item>

      </el-form>
    </el-dialog>


    <!-- 申请 -->
    <el-dialog title="修改备注"  :visible.sync="remarkOpen" width="500px" hei append-to-body>
      <el-form ref="applyFrom"  label-width="80px">

        <el-form-item label="修改备注" prop="applyReason">
          <el-input v-model="remarkParam.remark" placeholder="请输入备注" />
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer" >
        <el-button type="primary" @click="submitRemark">确 定</el-button>
        <el-button @click="remarkCancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listAttention, getAttention, delAttention, addAttention, updateAttention ,selectNotAttentionUserList,updateFriendApply} from "@/api/petSys/attention";
import {addAddFriend, listAddFriend, updateAddFriend} from "@/api/petSys/addFriend";
import {resetForm} from "@/utils/ruoyi";

export default {
  name: "Attention",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 好友列表表格数据
      attentionList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      applyOpen:false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        attentionUserId: null,
        beAttentionUserId: null,
        createTime: null,
        updateTime: null,
        isDelete: null,
        nickName:null

      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      friendList:[],
      applyList:[],
      applyflag:1,
      queryFrienName:"",
      remarkOpen:false,
      remarkParam:{
        id:"",
        remark:""
      }
    };
  },
  created() {

  },
  mounted() {
    this.userId = this.$store.state.user.id;
    console.log(this.$store.state.user.id)
    this.getList();
  },
  methods: {
    /** 查询好友列表列表 */
    getList() {
      this.loading = true;
      this.queryParams.attentionUserId = this.userId;
      listAttention(this.queryParams).then(response => {
        // this.attentionList = response.rows;
        if(this.queryParams.nickName !=null){
          this.attentionList=[];
          response.rows.forEach(item=>{
            // console.log(item)
            if( item.friendNickName.indexOf(this.queryParams.nickName)!= -1){

              this.attentionList.push(item);
            }
          })
        }else {
          this.attentionList = response.rows;
        }

        this.total = response.total;
        this.loading = false;
        this.queryParams.nickName =null;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        attentionUserId: null,
        beAttentionUserId: null,
        createTime: null,
        updateTime: null,
        isDelete: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },


    queryFreiend(){
      //todo 这里会查到自己和已经关注的人
      var params = {
        nickName:this.queryFrienName
      }
      console.log(this.queryFrienName)
      selectNotAttentionUserList(params).then(response=>{
        this.friendList = response.rows
        console.log(response)
      })
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加好友列表";
    },
    /**
     * 添加好友弹窗的添加朋友
     * @param row
     */
    addFriend(row){
      let data ={
        beApplyUserId:row.userId,
        applyUserId:this.userId,
        isAgree:0
      }
      addAddFriend(data).then(respone=>{
        this.$modal.msgSuccess("添加成功");
      })
      console.log(row)
    },

    handleApply(e){
      this.applyList = []
      this.applyOpen = true;
        let params = {

        }
        this.applyflag = e
      if(e==1){
        params.applyUserId = this.userId
      }else{
        params.beApplyUserId = this.userId
      }
      listAddFriend(params).then(response=>{

        this.applyList = response.rows
        console.log(response.rows)
      })
      console.log(e)

    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.remarkParam ={
        id:row.id,
        remark:row.remark
      }
        this.remarkOpen = true;


      },
      remarkCancel(){
        this.remarkOpen = false;
      },
    submitRemark(){
      console.log(this.remarkParam)
      updateAttention(this.remarkParam).then(response=>{
        this.$modal.msgSuccess("修改成功");
        this.getList()
        this.remarkCancel()
      })

    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateAttention(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addAttention(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除好友列表编号为"' + ids + '"的数据项？').then(function() {
        return delAttention(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('petSys/attention/export', {
        ...this.queryParams
      }, `attention_${new Date().getTime()}.xlsx`)
    },
    /**
     * 通过好友申请
     */
    updateFriendApply(e,row){
      // console.log(row)
      let data = {
        id:row.id,
        isAgree:e,
        applyUserId:row.applyUserId

      }
      console.log(data)
      updateFriendApply(data).then(response=>{

        this.handleApply(2);
        this.getList();
        this.$modal.msgSuccess("添加成功");
      })


    },
  }
};
</script>
